﻿<%@ WebHandler Language="C#" Class="HandlerMainPageArcitle" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.SessionState;
using Newtonsoft.Json;
public class HandlerMainPageArcitle : IHttpHandler, IRequiresSessionState/*IRequiresSessionState,Session用*/
{
    
    public void ProcessRequest (HttpContext context) {
        string strconn = LinkToDatabase.strConn;/*App_Code->UserClass.cs*/
        string strcmd = "";
        string type = context.Request.Form["type"];
        int UserId=0;
        if (context.Request.Cookies["u_id"] != null && context.Request.Cookies["u_id"].Value != "null" )
        {
           UserId = Convert.ToInt32(context.Request.Cookies["u_id"].Value);
        }
        SqlCommand cmd = null;
        using (SqlConnection conn = new SqlConnection(strconn))
        {
            switch (type)
            {
                //1=======================================================================
                case "1":
                    strcmd = @"select 
                                     [unboxing_article_title] as title
                                    ,[unboxing_article_content] as content
									,CONVERT(varchar, [unboxing_article_publish_date], 120 ) 
                                    ,[product_id] as p
                                    ,[dbo].[unboxing_article].[user_id] as u
									,[user_username] as name
									,[dbo].[unboxing_image].[image] as picture
                              from ([dbo].[unboxing_article] join [dbo].[user] 
							  on .[dbo].[unboxing_article].[user_id]=[dbo].[user].[user_id]) 
							  left join [dbo].[unboxing_image] 
							  on [dbo].[unboxing_article].[unboxing_article_id]=[dbo].[unboxing_image].[unboxing_article_id]
                              where [dbo].[unboxing_article].[unboxing_article_id]=@Id
                            ";
                    break;
                //2=======================================================================
                case "2":
                    strcmd = @"select 
                                     [dbo].[unboxing_article].[unboxing_article_id] as a
                                    ,[unboxing_article_title] as title
									,[dbo].[unboxing_image].[image] as picture
                              from ([dbo].[unboxing_article] join [dbo].[user] 
							  on [dbo].[unboxing_article].[user_id]=[dbo].[user].[user_id]) 
							  left join [dbo].[unboxing_image] 
							  on [dbo].[unboxing_article].[unboxing_article_id]=[dbo].[unboxing_image].[unboxing_article_id]
                              where [dbo].[unboxing_article].[user_id]=
                              (
							  select 
							  [dbo].[unboxing_article].[user_id] 
							  from [dbo].[unboxing_article]	
                              where [dbo].[unboxing_article].[unboxing_article_id]=@Id and [unboxing_visible]=1
							  )
							  order by [unboxing_article_publish_date] Desc
                              OFFSET @Num ROWS FETCH NEXT 10 ROWS ONLY 
                            ";
                    break;
                //3=======================================================================
                case "3":
                    strcmd = @"Select COUNT(*) as num
                               from [dbo].[unboxing_article] 
                               where [dbo].[unboxing_article].[user_id]= (
							  select 
							  [dbo].[unboxing_article].[user_id] 
							  from [dbo].[unboxing_article]	
                              where [dbo].[unboxing_article].[unboxing_article_id]=@Id
							  )
                            ";
                    break;
                    ;
                //4=======================================================================
                case "4":
                    strcmd = @"SELECT [product_id] as pid
                               ,[introduction] as content
                               FROM [UBOX].[dbo].[production_introduction]
                               where [product_id]=
                               (
                                select 
							    [dbo].[unboxing_article].[product_id] 
							    from [dbo].[unboxing_article]	
                                where [dbo].[unboxing_article].[unboxing_article_id]=@Id
                               )
                            ";
                    break;
                    ;
                //5=======================================================================
                case "5":
                    strcmd = @"SELECT 
                               [price]
                              ,[product_name] as name
                               FROM [UBOX].[dbo].[production_product]
                               where [product_id]=@Id
                            ";
                    break;
                    ;
                //6=======================================================================
                case "6":
                    strcmd = @"SELECT 
                             [introduction] as content
                             FROM [UBOX].[dbo].[production_introduction]
                             where [product_id]=@Id
                            ";
                    break;
                    ;
                //7=======================================================================
                case "7":
                    strcmd = @"SELECT [product_id]
                             ,[image] as picture
                             FROM [UBOX].[dbo].[production_image] 
                             where [product_id]=@Id
                             order by [image_order] desc
                            ";
                    break;
                //8=======================================================================
                case "8":
                    strcmd = @"DECLARE @Num INT
                    set @Num=(SELECT  COUNT(*)
                    FROM [UBOX].[dbo].[article_reader]
                    where [user_id]=@UserId and [unboxing_article_id]=@Id and [unboxing_browser_date]=CONVERT(NVARCHAR, GETDATE(), 112))
                    if @Num =0 
                    Begin
                         INSERT INTO [dbo].[article_reader] ([user_id],[unboxing_article_id] , [unboxing_browser_date])
                         VALUES (@UserId,@Id ,@Time);
                         set @Result = @@rowcount;
                    End
                    else   
                    Begin
                    set @Result=0;
                    End
                            ";
                    break;
                //9修~~~~~~~=======================================================================
                case "9":
                    strcmd = @"   
                             declare @Num bit,@Result int
                             set @Num=(SELECT  
                             [unboxing_visible]
                             FROM [UBOX].[dbo].[unboxing_article]
                             where [unboxing_article_id]=@Aid)
                             if(@Num=0 or @Num is NULL)
                             Begin
                                  set @Result=-1 
								  select @Result as Result
                             End
                             else
                             Begin
							      INSERT INTO [dbo].[message_board]
                                  ([message_content],[user_id],[message_date],[unboxing_article_id] , [message_visible])
                                  OUTPUT Inserted.[message_id] as Result
                                  VALUES (@Content,@Uid ,@Date,@Aid,@Visible);
                            End
                            ";
                    break;
                //10=======================================================================
                case "10":
                    strcmd = @"
                               SELECT [message_content] as content 
                               ,[user_username] as name
                               ,CONVERT(varchar, [message_date], 120 )  as date
                               ,[user_picture] as picture
                               ,[message_id] as mid
                               ,[dbo].[message_board].[user_id] uid
                               FROM [UBOX].[dbo].[message_board] join [dbo].[user] on [dbo].[message_board].[user_id]=[dbo].[user].[user_id]
                               where [message_id]=@Id and [message_visible]=1
                            ";
                    break;
                //11=======================================================================
                case "11":
                    strcmd = @"
                               SELECT [message_content] as content 
                               ,[user_username] as name
                               ,CONVERT(varchar, [message_date], 120 )  as date
                               ,[user_picture] as picture
                               ,[message_id] as mid
                               ,[dbo].[message_board].[user_id] uid
                               FROM [UBOX].[dbo].[message_board] join [dbo].[user] on [dbo].[message_board].[user_id]=[dbo].[user].[user_id]
                               where [unboxing_article_id]=@Id and [message_visible]=1
                               order by [message_date] Desc
                               OFFSET @JumpNum ROWS FETCH NEXT @Num ROWS ONLY 
                            ";
                    break;
                //12=======================================================================
                case "12":
                    strcmd = @"DECLARE @Num INT
                    set @Num=(SELECT  COUNT(*)
                    FROM [UBOX].[dbo].[user_favorite_box]
                    where [user_id]=@UserId and [unboxing_article_id]=@Id)
                    if @Num =0 
                    Begin
                         INSERT INTO [dbo].[user_favorite_box] ([user_id],[unboxing_article_id])
                         VALUES (@UserId,@Id);
                         set @Result = @@rowcount;
                    End
                    else   
                    Begin
                    set @Result=0;
                    End
                    select @Result as Result
                            ";
                    break;
                //13=======================================================================
                case "13":
                    strcmd = @"SELECT [product_id] as pid
                              ,[category_id] as cid
                              FROM [UBOX].[dbo].[production_product]
                              where[product_id]=
                             (SELECT  [product_id]
                              FROM [UBOX].[dbo].[unboxing_article] 
                              where [unboxing_article_id]=@Id)
                            ";
                    break;
                //14=======================================================================
                case "14":
                    strcmd = @"SELECT count(*) as datanum
                               FROM [UBOX].[dbo].[message_board]
                               where [unboxing_article_id]=@Id and [message_visible]=1
                            ";
                    break;
                //14=======================================================================
                case "15":
                    strcmd = @"
                     DECLARE @Num INT set @Num=(SELECT  COUNT(*)
                         FROM [dbo].[message_board]
                         where [message_id]=@Id and [message_visible]=1)
                    if @Num =1
					Begin 
                         set @Num=(SELECT  COUNT(*)
                         FROM [dbo].[user_report] 
                         where [dbo].[user_report].[user_id]=@UserId and [report_message_id]=@Id and [report_date]=CONVERT(NVARCHAR, GETDATE(), 112))
                         if @Num =0 
                         Begin
                              INSERT INTO [dbo].[user_report] ([report_message_id],[user_id],[report_content],[report_date])
                               VALUES (@Id,@UserId,@Content,CONVERT(NVARCHAR, GETDATE(), 112));
                               set @Result = @@rowcount;
                         End
                         else   
                         Begin
                              set @Result=0;
                         End
					End
					else   
                         Begin
                              set @Result=0;
                         End
                    select @Result as Result
                            ";
                    break;
                case "16":
                    strcmd = @"
                       DECLARE @Num INT 
                       set @Num=(SELECT  COUNT(*)
                       FROM [dbo].[unboxing_article]
                       where [unboxing_article_id]=@Id and [unboxing_visible]=1)
                       if @Num =1
					   Begin 
                            set @Num=(SELECT  COUNT(*)
                            FROM [dbo].[user_report] 
                            where [dbo].[user_report].[user_id]=@UserId and [report_unboxing_id]=@Id and [report_date]=CONVERT(NVARCHAR, GETDATE(), 112))
                            if @Num =0 
                            Begin
                                 INSERT INTO [dbo].[user_report] ([report_unboxing_id],[user_id],[report_content],[report_date])
                                 VALUES (@Id,@UserId,@Content,CONVERT(NVARCHAR, GETDATE(), 112));
                                 set @Result = @@rowcount;
                            End
                            else   
                            Begin
                                 set @Result=0;
                            End
					   End
					   else   
                       Begin
                             set @Result=0;
                       End
                       select @Result as Result
                            ";
                    break;
                case "17":
                    strcmd = @"
                     declare @Num bit,@Result bit
                             set @Num=(SELECT  
                             [message_visible] 
                             FROM [UBOX].[dbo].[message_board]
                             where [message_id]=@Id)
                             if(@Num=0 or @Num is NULL)
                             Begin
                                  set @Result=0
                             End
                             else
                             Begin
                                  UPDATE [dbo].[message_board]
                                  set [message_visible]  = 0
                                  where [message_id]=@Id
	                              set @Result=1
                             End
                             select @Result as Result
                            ";
                    break;
                case "18":
                    strcmd = @"
                     declare @Num bit,@Result bit
                             set @Num=(SELECT  
                             [unboxing_visible]
                             FROM [UBOX].[dbo].[unboxing_article]
                             where [unboxing_article_id]=@Id)
                             if(@Num=0 or @Num is NULL)
                             Begin
                                  set @Result=0
                             End
                             else
                             Begin
                                  UPDATE [dbo].[unboxing_article]
                                  set [unboxing_visible]  = 0
                                  where [unboxing_article_id]=@Id
	                              set @Result=1
                             End
                             select @Result as Result
                            ";
                    break;
            }
            using (SqlDataAdapter da = new SqlDataAdapter(strcmd, conn))
            {
                SqlParameter Result;
                Result = new SqlParameter("@Result", SqlDbType.Bit);
                Result.Direction = ParameterDirection.Output;
                switch (type)
                {
                    //1,3,4,5,6,7,10,11,13=======================================================================
                    case "1":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "10":
                    case "13":
                    case "14":
                    case "17":
                    case "18":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id",context.Request.Form["Target"]);
                        break;
                    case "2":
                    //2=======================================================================
                        da.SelectCommand.Parameters.AddWithValue("@Id",context.Request.Form["Target"]);
                        da.SelectCommand.Parameters.AddWithValue("@Num", (Convert.ToInt32(context.Request.Form["PageNum"])-1)*10);
                        break;
                    //8=======================================================================
                    case "8":
                        //input
                        cmd = new SqlCommand(strcmd, conn);
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@Id", context.Request.Form["Target"]);
                        cmd.Parameters.AddWithValue("@UserId", UserId);
                        cmd.Parameters.AddWithValue("@Time", DateTime.Now);
                        //output
                        cmd.Parameters.Add(Result);
                        break;
                        //9=======================================================================
                    case "9":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Content", context.Request.Form["Content"].Replace("src=%22/Uploads/", "src=%22./Uploads/"));
                        da.SelectCommand.Parameters.AddWithValue("@UId", UserId);
                        da.SelectCommand.Parameters.AddWithValue("@Date", DateTime.Now);
                        da.SelectCommand.Parameters.AddWithValue("@Aid", context.Request.Form["Target"]);
                        da.SelectCommand.Parameters.AddWithValue("@Visible", 1);
                        break;
                    case "11":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id", context.Request.Form["Target"]);
                        da.SelectCommand.Parameters.AddWithValue("@Num", Convert.ToInt32(context.Request.Form["Num"]));
                        da.SelectCommand.Parameters.AddWithValue("@JumpNum", (
                            Convert.ToInt32(context.Request.Form["Currentlypage"]) - 1) 
                            * Convert.ToInt32(context.Request.Form["Num"]));
                       
                        break;
                    case "12":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id", context.Request.Form["Target"]);
                        da.SelectCommand.Parameters.AddWithValue("@UserId", UserId);
                        //output
                        da.SelectCommand.Parameters.Add(Result);   
                        break;
                    case "15":
                    case "16":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id", context.Request.Form["Target"]);
                        da.SelectCommand.Parameters.AddWithValue("@UserId", UserId);
                        da.SelectCommand.Parameters.AddWithValue("@Content", context.Request.Form["content"]);
                        //output
                        da.SelectCommand.Parameters.Add(Result);
                        break;
                }
                DataSet ds = new DataSet();
                switch (type)
                {
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "9":
                    case "10":
                    case "11":
                    case "12":
                    case "13":
                    case "14":
                    case "15":
                    case "16":
                    case "17":
                    case "18":
                        da.Fill(ds);
                        context.Response.ContentType = "application/json";
                        context.Response.Write(JsonConvert.SerializeObject(ds));
                        break;
                    case "8":  
                        try{
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            context.Response.ContentType = "text/plain";
                            if (Convert.ToBoolean(Result.Value)){
                                context.Response.Write("True");
                            }
                            else{
                                context.Response.Write("False");
                            }
                        }
                        catch{
                            context.Response.Write("False");//傳context.Request.Form[""]不需加單引號
                        }
                        cmd.Dispose();
                        break;
                    case "19":
                        if (context.Request.Cookies["u_id"] != null && context.Request.Cookies["u_id"].Value.ToString() != "null" && context.Session["login"].ToString() == "ok")
                        {
                            context.Response.Write("True");
                        }
                        else
                        {
                            context.Response.Write("False");
                        }
                        break; 
                }
                conn.Close();
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}